package com.ruoyi.user.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.user.AppDressInfo;
import com.ruoyi.user.mapper.DressMapper;
import com.ruoyi.user.model.constant.UserConstant;
import com.ruoyi.user.model.pojo.entity.Dress;
import com.ruoyi.user.model.pojo.entity.DressInfo;
import com.ruoyi.user.model.pojo.entity.DressUser;
import com.ruoyi.user.model.pojo.request.app.AppDressSearchRequest;
import com.ruoyi.user.model.pojo.request.app.dress.AppDressUseRequest;
import com.ruoyi.user.model.pojo.request.crm.CrmDressAddOrEditRequest;
import com.ruoyi.user.model.pojo.request.crm.CrmDressSearchRequest;
import com.ruoyi.user.model.pojo.result.app.AppDressInfoResult;
import com.ruoyi.user.service.IDressInfoService;
import com.ruoyi.user.service.IDressService;
import com.ruoyi.user.service.IDressUserService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 装扮中心 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-16
 */
@Service
public class DressServiceImpl extends ServiceImpl<DressMapper, Dress> implements IDressService {


    @Autowired
    private IDressInfoService iDressInfoService;
    @Autowired
    private IDressUserService dressUserService;
    @Autowired
    private DressMapper dressMapper;

    @Override
    public Pages<AppDressInfoResult> listPage(AppDressSearchRequest request, Long userId) {
        Page<Dress> dressPage = PageUtils.initPage(request, Dress.class);
        Page<Dress> page = lambdaQuery()
                .eq(!CommonUtils.isNull(request.getDressType()), Dress::getDressType, request.getDressType())
                .orderByAsc(Dress::getId)
                .page(dressPage);
        return PageUtils.pages(dressPage, getInfo(page.getRecords(), userId, request.getDressType()));
    }

    @Override
    public boolean use(AppDressUseRequest request, Long userId) {

        dressUserService.lambdaUpdate()
                .ne(DressUser::getDressId, request.getId())
                .eq(DressUser::getUserId, userId)
                .set(DressUser::getIsUse, UserConstant.AppStatus.no)
                .update();

        dressUserService.lambdaUpdate()
                .eq(DressUser::getDressId, request.getId())
                .eq(DressUser::getUserId, userId)
                .set(DressUser::getIsUse, UserConstant.AppStatus.yes)
                .update();
        return true;
    }

    @Override
    public List<AppDressInfo> userUseDressList(Long userId) {
        return dressMapper.userUseDressList(userId);
    }

    @Override
    public boolean addOrEdit(CrmDressAddOrEditRequest request) {
        Dress dress = getById(request.getId());
        if (CommonUtils.isNull(dress)) {
            dress = new Dress();
            dress.setCreateTime(new Date());
        }
        BeanUtils.copyProperties(request, dress);
        dress.setStatus(UserConstant.AppStatus.yes);
        return saveOrUpdate(dress);
    }

    @Override
    public List<Dress> crmListPage(CrmDressSearchRequest request) {
        return lambdaQuery()
                .eq(!CommonUtils.isNull(request.getDressType()), Dress::getDressType, request.getDressType())
                .orderByAsc(Dress::getId)
                .list();
    }

    List<AppDressInfoResult> getInfo(List<Dress> dress, Long userId, Integer dressType) {
        List<DressUser> use = dressUserService.isUse(userId, dressType);
        return dress.stream().map(res -> {
            DressUser user = null;
            for (DressUser dressUser : use) {
                if (dressUser.getDressId().equals(res.getId())) {
                    user = dressUser;
                }
            }
            List<DressInfo> list = iDressInfoService.lambdaQuery()
                    .eq(DressInfo::getDressId, res.getId())
                    .orderByAsc(DressInfo::getDayNum).list();
            return AppDressInfoResult.builder()
                    .dressImg(res.getDressImg())
                    .dressInfos(list)
                    .residueDay(!CommonUtils.isNull(user) ? DateUtils.timeByDay(user.getDressTime(), new Date()) : 0)
                    .dressType(res.getDressType())
                    .grade(res.getGrade())
                    .isUse(!CommonUtils.isNull(user) ? user.getIsUse() : -1)
                    .id(res.getId())
                    .name(res.getName())
                    .price(res.getPrice())
                    .status(res.getStatus())
                    .video(res.getVideo())
                    .build();
        }).collect(Collectors.toList());
    }


}
